package com.hl.sfzy.controller;

import com.github.pagehelper.PageInfo;
import com.hl.sfzy.entity.ArticleType;
import com.hl.sfzy.service.ArticleTypeServcie;
import com.hl.sfzy.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/articleType")
public class ArticleTypeController {
    @Autowired
    private ArticleTypeServcie articleTypeServcie;
    @PostMapping("/add")
    public R add(@RequestBody ArticleType articleType){
        int i = articleTypeServcie.add(articleType);
        if(i>0){
            return R.ok("添加成功");
        }
        return R.fail("添加失败");
    }
    @DeleteMapping("/{id}")
    public R remove(@PathVariable Long id){
        int i = articleTypeServcie.remove(id);
        if(i>0){
            return R.ok("删除成功");
        }
        return R.fail("删除失败");
    }
    @PutMapping("/edit")
    public R edit(@RequestBody ArticleType articleType){
        int i = articleTypeServcie.edit(articleType);
        if(i>0){
            return R.ok("修改成功");
        }
        return R.fail("修改失败");
    }
    @GetMapping("/{id}")
    public R findById(@PathVariable Long id){
        ArticleType articleType = articleTypeServcie.findById(id);
        return R.ok(articleType);
    }
    @GetMapping("/findAll")
    public R findAll(
            @RequestParam(required = false,defaultValue = "1") int pageNum,
            @RequestParam(required = false,defaultValue = "5") int pageSize,
            ArticleType articleType){
        PageInfo<ArticleType> articleTypes = articleTypeServcie.findAll(pageNum, pageSize, articleType);
        return R.ok(articleTypes);
    }
    @DeleteMapping("/deleteBatch")
    public R deleteBatch(@RequestBody List<Long> ids){
        int i = articleTypeServcie.deleteBatch(ids);
        if(i>0){
            return R.ok("删除"+i+"条数据");
        }else
            return R.fail("删除失败");
    }
}
